package com.example.excel;

import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.util.IOUtils;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;

public class ExcelWrite {

    public static void writeExcel(List<List<Object>> excelList, InputStream inputStream, String filepath){
        Workbook workbook = null;
        try {
            workbook = WorkbookFactory.create(inputStream);
            int sheetsNumber = workbook.getNumberOfSheets();
            CellStyle style = workbook.createCellStyle();
            style.setFillBackgroundColor(HSSFColor.RED.index);
            for (int n = 0; n < sheetsNumber; n++) {
                List sheetList = excelList.get(n);
                if(sheetList==null) continue;

                Sheet sheet = workbook.getSheetAt(0);
                Row row = null;
//                for (int i = 0; i < sheetList.size(); i++) {
//                    Object obj = sheetList.get(i);
//                    row = sheet.getRow(i);
//                    Cell createCell = row.getCell(5);
//                    if(createCell==null){
//                        row.createCell(5);
//                        createCell = row.getCell(5);
//                    }
//                    createCell.setCellValue(res.getMessage());
//                }
            }
            FileOutputStream outputStream = null;
            try {
                outputStream = new FileOutputStream(filepath);
                workbook.write(outputStream);
            } finally {
                if (outputStream != null) {
                    outputStream.close();
                }
                if (workbook != null) {
                    workbook.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(workbook);
            IOUtils.closeQuietly(inputStream);
        }
    }

}
